<template>
  <div id="changedialog">
    <commonformdialog
      :isshowDialogs="isshowDialog"
      :dialogTitle="dialogTitle"
      @closetextDialog="closetextDialog"
      @confirmbtn="confirmbtn"
      :dialogWidth="'750px'"
    >
      <div class="innerLoding">
        <el-row>
          <el-form
            :model="formData"
            :rules="rules"
            ref="ruleForm"
            label-width="120px"
            size="small"
          >
            <el-row>

               <el-col :span="20">
                <el-form-item label="地点名称：" prop="title">
                  <el-input placeholder="请输入地点名称" v-model="formData.title">
                    <template slot="append"
                      ><span @click="showMapSelecte">选择</span></template
                    >
                  </el-input>
                </el-form-item>
              </el-col>
                <el-col :span="20">
              <el-form-item label="所在城市：" prop="city_id">
                <el-cascader
                  ref="cascader"
                  filterable
                   placeholder="请选择所在城市"
                  v-model="formData.city_id"
                  :options="cityoptions"
                  :props="{ expandTrigger: 'hover', checkStrictly: true ,emitPath:false}"
                  clearable
                  :show-all-levels="false"
                  style="width: 100%"
                ></el-cascader>
              </el-form-item>
            </el-col>
            <el-col :span="20">
                <el-form-item label="是否采集相片:" prop="is_photo">
                  <el-radio-group v-model="formData.is_photo">
                    <el-radio :label="1">是</el-radio>
                    <el-radio :label="0">否</el-radio>

                  </el-radio-group>
                </el-form-item>
              </el-col>
              <el-col :span="20">
                <el-form-item label="备注：" prop="description">
                  <el-input type="textarea" :rows="3" resize="none" placeholder="请输入备注" v-model="formData.description">
                  </el-input>
                </el-form-item>
              </el-col>
    
            </el-row>
          </el-form>
        </el-row>
      </div>
    </commonformdialog>
    <selecteMap
      :mapData="mapData"
      :showmap="isshowDialogMAP"
      :maptitle="dialogTitleMAP"
      @closetextDialogMap="closetextDialogMAP"
      @confirmbtnMap="confirmbtnMAP"
    ></selecteMap>
  </div>
</template>

<script type="text/javascript">
//引入弹窗
import commonformdialog from "@/components/commondialog/commondialog";

import selecteMap from "@/components/selecteMap";
import { preparePointCreate, preparePointUpdate } from "@/api/dataPreparation";
export default {
  data() {
    return {
      showPicker: false,
      multipleopen: true,
      formData: {
        is_photo:0,
        city_id: "",
        title: "",
        description: "",
      },
      rules: {
        title: [
          { required: true, message: "请输入地点名称", trigger: "blur" },
        ],
        city_id: [
          { required: true, message: "请选择城市", trigger: "blur" },
        ],


      },
      isshowDialogMAP: false,
      dialogTitleMAP: "选择地址",
      mapData: {},
      selecte: true,
    };
  },
  props: {
    //是否显示弹窗
    isshowDialog: {
      type: Boolean,
      default: false,
      required: true,
    },
    //表单的名字
    dialogTitle: {
      type: String,
      default: "",
      required: true,
    },
    //是编辑还是新增
    formType: {
      type: Number,
      default: 0,
    },
    cityoptions: {
      type: Array,
    },
    dialogdata: {
      type: Object,
    },
  },
  components: {
    commonformdialog,
    selecteMap,
  },
  methods: {
    showMapSelecte() {
      this.isshowDialogMAP = true;
      if (this.formType == 1) {
        this.$nextTick(() => {
          let mapData = {};
          mapData.lat = this.formData.lat;
          mapData.lng = this.formData.lng;
          mapData.title = this.formData.title;
          this.mapData = mapData;
        });
      }
    },
    closetextDialogMAP() {
      this.isshowDialogMAP = false;
    },
    confirmbtnMAP(val) {
      if (val) {
        this.formData.lat = val.lat;
        this.formData.lng = val.lng;
         if(this.formData.title==''){
          this.formData.title = val.address;
        }
      }
      this.isshowDialogMAP = false;
    },
    closetextDialog() {
      this.$emit("closedialog");
    },

    confirmbtn() {
      this.$refs["ruleForm"].validate(async (valid) => {
        if (!valid) return;
        let params = this.formData;
        let data;
        if (this.formType == 0) {
          data = await preparePointCreate(params);
        } else {
          data = await preparePointUpdate(params);
        }
        if (data.code == "0000") {
          this.$message({
            type: "success",
            message: data.msg,
          });
          this.$emit("confirm");
        }
      });
    },
  },
  watch: {
    isshowDialog: function (val) {
      if (!val) {
        this.$refs["ruleForm"].resetFields();
      }
    },
    dialogdata: function (val) {
      this.formData = val;
    },
  },
  created() {},
};
</script>

<style lang="scss" scoped>
::v-deep {
  #InputSearch {
    display: none !important;
  }
  .grid-emojis {
    grid-template-columns: repeat(10, 10%) !important;
  }
  .small {
    position: absolute;
    z-index: 9;
    left: -45px;
    top: 30px;
  }
  .el-input-group__append {
    cursor: pointer;
  }
}
</style>
